<template>
  <div>
    <div class="security-all">
      <div class="securityHeader">
        <p class="text-box">
          <span>安全 / </span>
          <span class="text">API访问密钥</span>
          <span> - 使用访问密钥向SenseCAP API服务发出安全的REST或HTTP请求</span>
        </p>
        <el-button text bg type="primary" @click="clickAdd">新增</el-button>
      </div>

      <!-- 表单 -->
      <el-table :data="showList" stripe class="table-box minh-500">
        <el-table-column align="center" type="index" label="序号" width="100" />
        <el-table-column align="center" prop="tag" label="标签" />
        <el-table-column align="center" prop="id" label="密钥ID" />
        <el-table-column align="center" prop="authority" label="访问权限" />
        <el-table-column
          align="center"
          prop="online"
          :formatter="fonline3"
          label="密钥状态"
        />
        <el-table-column
          align="center"
          prop="cTime"
          :formatter="fcTime"
          label="创建时间"
        />
        <el-table-column
          align="center"
          prop="uTime"
          :formatter="fcTime"
          label="更新时间"
        />

        <el-table-column align="center" label="操作">
          <template #default="scope">
            <!-- <el-button type="danger" round>Danger</el-button> -->
            <el-row class="el-rowsss">
              <el-button
                type="primary"
                @click="clickEdit(scope)"
                :icon="Edit"
                circle
              />
              <el-button
                type="danger"
                @click="clickDel(scope)"
                :icon="Delete"
                circle
              />
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 添加信息弹窗 -->
    <el-dialog
      v-model="centerDialogVisible"
      title="新增密钥"
      width="30%"
      @close="closeDialog"
      center
    >
      <el-form
        :label-position="'right'"
        label-width="100px"
        :model="formLabelAlign"
        style="max-width: 460px"
      >
        <el-form-item v-if="formLabelAlign.id" label="密钥id">
          <el-input disabled v-model="formLabelAlign.id" />
        </el-form-item>
        <el-form-item label="标签">
          <el-input v-model="formLabelAlign.tag" />
        </el-form-item>
        <el-form-item label="访问权限">
          <el-input v-model="formLabelAlign.authority" />
        </el-form-item>
        <el-form-item label="密钥状态" prop="online">
          <el-select v-model="formLabelAlign.online" placeholder="输入状态">
            <el-option label="使用中" value="true" />
            <el-option label="未使用" value="false" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm(formLabelAlign)"
            >确认</el-button
          >
          <el-button @click="cancelForm">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog v-model="showDel" title="温馨提示" width="30%" align-center>
      <span>真的要删除吗</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showDel = false">取消</el-button>
          <el-button type="primary" @click="certainDel"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { Delete, Edit } from "@element-plus/icons-vue";
import { ref, reactive, onMounted, inject } from "vue";
import * as api from "@/api/index.js";

//导入格式化时间的公共方法
// const utils = inject('utils');
const { fcTime, fonline3 } = inject("utils");

//定义和获取安全数据列表
let securityList = ref([]);
//定义展示列表
let showList = ref([]);
const getSecurityList = () => {
  api.$securityList().then((res) => {
    securityList.value = res.data;
    showList.value = res.data;
  });
};

//定义一个变量,add时候添加,edit时为删除
let editOrAdd = ref("add");

//新增弹窗
let centerDialogVisible = ref(false);
//点击弹窗的关闭按钮清空表单内的内容
const closeDialog = () => {
  resetForm();
};

//弹窗表单
const formLabelAlign = reactive({
  id: "",
  tag: "",
  authority: "",
  online: "false",
});
//点击提交
const submitForm = (obj) => {
  // debugger;
  if (!obj.tag) {
    alert("请输入正确的标签");
  } else if (!obj.authority) {
    alert("请输入正确的权限值");
  } else {
    if (editOrAdd.value == "add") {
      api.$securityAdd(obj).then((res) => {
        securityList.value.unshift(res.data);
        centerDialogVisible.value = false;
        //重置表单值
        resetForm();
      });
    } else if (editOrAdd.value == "edit") {
      //当editOrAdd的值为edit时候发送编辑
      api.$securityEdit(obj).then((res) => {
        // securityList.value.unshift(res.data);
        showList.value.splice(editIndex.value, 1, res.data);
        centerDialogVisible.value = false;
        //重置表单值
        resetForm();
      });
    }
  }
};

//点击取消
const cancelForm = () => {
  centerDialogVisible.value = false;
  resetForm();
};

//重置表单内容
const resetForm = () => {
  formLabelAlign.online = "false";
  formLabelAlign.id = "";
  formLabelAlign.tag = "";
  formLabelAlign.authority = "";
};

//点击新增按钮
const clickAdd = () => {
  centerDialogVisible.value = true;
  editOrAdd.value = "add";
};

//获取编辑的下标
let editIndex = ref(0);
//点击编辑
const clickEdit = (r) => {
  editIndex.value = r.$index;
  console.log(editIndex.value);
  editOrAdd.value = "edit";
  formLabelAlign.id = r.row.id;
  formLabelAlign.tag = r.row.tag;
  formLabelAlign.authority = r.row.authority;
  formLabelAlign.online = r.row.online;
  // console.log(formLabelAlign);
  centerDialogVisible.value = true;
};

//删除的id和下标
let deleteId = 0;
let deletIndex = 0;
//删除元素
const showDel = ref(false);
//点击删除
const clickDel = (item) => {
  console.log(item.$index);
  deleteId = item.row.id;
  showDel.value = true;
  deletIndex = item.$index;
};
//确认删除
const certainDel = () => {
  let data = {
    id: deleteId,
  };
  api.$securityDel(data).then((res) => {
    console.log(res);
    if (res.code === 666) {
      showDel.value = false;
      // getSecurityList();
      showList.value.splice(deletIndex, 1);
    }
  });
};

//挂载
onMounted(() => {
  getSecurityList();
});
</script>




<style lang="less" scoped>
.security-all {
  margin: 15px;
  font-size: 16px;

  .securityHeader {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #fff;
    padding: 15px;
    .text-box {
      .text {
        font-weight: bold;
      }
    }
  }
  .table-box {
    margin-top: 15px;
  }

  .dialog-footer button:first-child {
    margin-right: 10px;
  }

  .el-rowsss {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>